<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*, core.*, util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    input[type=file] { width: 500px; }
    table.fileAttachList { width: auto; margin-bottom: 0;}
    table.fileAttachList tr:first-child { background-color: #eee; }
    table.fileAttachList td:nth-child(1) { min-width: 400px; }
    table.fileAttachList td:nth-child(2) { text-align: right; min-width: 80px; }
    table.fileAttachList td:nth-child(3) { text-align: center; }
    table.fileAttachList tr:last-child { background-color: #eee; }
    td > form { margin: 0; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int articleId = param.getInt("aid", 0);
    String cmd = param.getString("cmd", "");

    Article article = ArticleDAO.selectById(articleId);
    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canEditArticle(article))
        new UnauthorizedAccessException("게시글 편집 권한 없음");

    if (cmd.equals("delete")) {
        int id = param.getInt("id", 0);
        FileDAO.delete(id);
    }
    ArrayList<File> list;
    if (articleId > 0)
        list = FileDAO.selectByArticleId(articleId);
    else
        list = FileDAO.selectOrphanByUserId(boardService.getCurrentUserId());
    String returnUrl = UrlUtil.getFullURL(request);
%>
<script>
    $(function () {
        if (window.parent) {
            var h = $("html").height();
            $('iframe[src*=file]', window.parent.document).css("height", h);
        }
    });

    function uploadFormSubmit() {
        $("img").show();
        $("#uploadForm").submit()
    }
</script>
<body>

<table class="table table-bordered table-condensed fileAttachList">
    <% if (list.size() > 0) { %>
        <tr><td>첨부파일</td><td>파일크기</td><td></td></tr>
    <% } %>
    <% for (File file : list) { %>
        <% String downUrl = FileDownloadServlet.makeUrl(request, boardId, articleId, file.getId()); %>
        <tr>
            <td><%= file.getFileName() %></td>
            <td>
                <% if (file.getFileSize() >= 10 * 1024 * 1024) { %>
                    <%= String.format("%d mb", file.getFileSize() / 1024 / 1024) %>
                <% } else if (file.getFileSize() >= 10 * 1024) { %>
                    <%= String.format("%d kb", file.getFileSize() / 1024) %>
                <% } else { %>
                    <%= file.getFileSize() %> bytes
                <% } %>
            </td>
            <td>
                <form method="post">
                    <input type="hidden" name="id" value="<%= file.getId() %>">
                    <input type="hidden" name="aid" value="<%= articleId %>" />
                    <button type="submit" class="btn btn-mini" name="cmd" value="delete"
                            onclick="return confirm('삭제하시겠습니까?')">삭제</button>
                    <a class="btn btn-mini" href="<%= downUrl %>">다운로드</a>
                </form>
            </td>
        </tr>
    <% } %>
    <tr>
        <td colspan="3">
            <% String upUrl = FileUploadServlet.makeUrl(request, boardId, articleId); %>
            <form id="uploadForm" method="post" action="<%= upUrl %>" enctype="multipart/form-data">
                <input type="hidden" name="bid" value="<%= boardId %>" />
                <input type="hidden" name="aid" value="<%= articleId %>" />
                <input type="hidden" name="returnUrl" value="<%= returnUrl %>" />
                <input type="file" name="file" onchange="uploadFormSubmit()"/>
                <img src="<%=request.getContextPath()%>/res/images/wait-small.gif" style="display:none"/>
            </form>
        </td>
    </tr>
</table>

</body>
</html>
